package com.study.servlet;

import com.github.pagehelper.PageHelper;
import com.study.mapper.SingerMapper;
import com.study.pojo.Singer;
import com.study.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/app")
public class ControllrServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = null;
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if("singerList".equals(type)){
             sqlSession = SqlSessionUtils.openSession();
            SingerMapper mapper = sqlSession.getMapper(SingerMapper.class);
            String pagecount = request.getParameter("pagecount");
            if(pagecount == null){
                 pagecount = "1";
                request.setAttribute("pagecount",pagecount);
            }
            else{
                request.setAttribute("pagecount",pagecount);

            }
            
            PageHelper.startPage(Integer.parseInt(pagecount), 3);
            List<Singer> allSinger = mapper.getAllSinger();


            request.setAttribute("singerList",allSinger);
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }
        else if("singerAdd".equals(type)){
           sqlSession = SqlSessionUtils.openSession();
            String singerNo = request.getParameter("SiNo");
            String singerName = request.getParameter("SiName");
            String singerArea =request.getParameter("SiArea");
            String singerGender = request.getParameter("SiSex");
            String singerRemark = request.getParameter("SiRemark");
            Singer s =new Singer();
            s.setSingerArea(singerArea);
            s.setSingerGender(singerGender);
            s.setSingerName(singerName);
            s.setSingerRemark(singerRemark);
            s.setSingerNo(singerNo);
            int i = sqlSession.getMapper(SingerMapper.class).insertSinger(s);
            if(i==1){
                sqlSession.commit();
                request.getRequestDispatcher("app?type=singerList").forward(request,response);
            }
            else{
                response.getWriter().write("您的网络错误，请稍后再试");
            }
        }
        else if("singerEdit".equals(type)){
             sqlSession = SqlSessionUtils.openSession();
            String singerId = request.getParameter("singerId");
            SingerMapper mapper = sqlSession.getMapper(SingerMapper.class);
            Singer singer = mapper.getSingerById(singerId);
            request.setAttribute("singer",singer);
            request.getRequestDispatcher("singerEdit.jsp").forward(request,response);
        }
        else if("update".equals(type)){
            sqlSession = SqlSessionUtils.openSession();
            String singerNo = request.getParameter("SiNo");
            String singerName = request.getParameter("SiName");
            String singerArea =request.getParameter("SiArea");
            String singerGender = request.getParameter("gender");
            String singerRemark = request.getParameter("SiRemark");
            Singer s =new Singer();
            s.setSingerNo(singerNo);
            s.setSingerArea(singerArea);
            s.setSingerGender(singerGender);
            s.setSingerName(singerName);
            s.setSingerRemark(singerRemark);
            int i = sqlSession.getMapper(SingerMapper.class).updateSinger(s);
            if(i==1){
                sqlSession.commit();
                request.getRequestDispatcher("app?type=singerList").forward(request,response);
            }
            else{
                response.getWriter().write("您的网络错误，请稍后再试");
            }
        }
        else if("singerDelete".equals(type)){
             sqlSession = SqlSessionUtils.openSession();
            String singerNo = request.getParameter("singerId");
            int i = sqlSession.getMapper(SingerMapper.class).deleteSingerById(singerNo);
            if(i==1){
                sqlSession.commit();
                request.getRequestDispatcher("app?type=singerList").forward(request,response);
            }
            else{
                response.getWriter().write("您的网络错误，请稍后再试");
            }
        }
        else if("singerDetail".equals(type)){
          sqlSession = SqlSessionUtils.openSession();
            String singerId = request.getParameter("singerId");
            Singer singer = sqlSession.getMapper(SingerMapper.class).getSingerById(singerId);
            request.setAttribute("singer",singer);
            request.getRequestDispatcher("singerDetail.jsp").forward(request,response);
        }
        SqlSessionUtils.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
